package com.itheima.sh.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * @Author 张伟奇
 * @Date 2021/5/5 19:45
 * @Version 1.0
 **/
public class MybatisUtils {
    private static InputStream is;
    private static  SqlSession sqlSession;

    public static SqlSession getSqlSession()throws Exception{
         is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory build = builder.build(is);
         sqlSession = build.openSession(true);
        return sqlSession;
    }

    public static void close()throws Exception{
        is.close();
        sqlSession.close();
    }
}
